package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.UserTokenMapper;
import com.qianfeng.openplatform.webmaster.pojo.UserToken;
import com.qianfeng.openplatform.webmaster.pojo.UserTokenExample;
import com.qianfeng.openplatform.webmaster.service.AppTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/23 23:11
 */
@Service
public class AppTokenServiceImpl implements AppTokenService {

    @Autowired
    private UserTokenMapper userTokenMapper;

    @Override
    public LayuiDataTableSource getApiTokenMgsToPage(Integer page, Integer limit, String accessToken) {
        PageHelper.startPage(page,limit);
        UserTokenExample  example =new UserTokenExample();
        UserTokenExample.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(accessToken)){
            c.andAccessTokenLike("%" + accessToken+"%");
        }
        List<UserToken> userTokenList = userTokenMapper.selectByExample(example);
        PageInfo<UserToken> info = new PageInfo<>(userTokenList);
        LayuiDataTableSource source = new LayuiDataTableSource();
        source.setCount(info.getTotal());
        source.setData(info.getList());
        return source;
    }

    @Override
    public OpenApiMaster updateToken(UserToken userToken) {
        int code = userTokenMapper.updateByPrimaryKeySelective(userToken);
        if (code>0){
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("更改失败....");
    }

    @Override
    public OpenApiMaster delToken(Integer[] ids) {
        int code= userTokenMapper.deleteByPrimaryKeyAll(ids);
        if (code>0){
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("更改失败....");
    }

    }

